<template>
    <div>
        <div>
            <el-table :data="userList" border style="width: 100%">
                <el-table-column prop="uid" label="用户编号">
                </el-table-column>
                <el-table-column prop="username" label="用户名称">
                </el-table-column>
                <el-table-column prop="rolename" label="所属角色">
                </el-table-column>
                <el-table-column prop="status" label="状态">
                    <template #default="item">
                        <div>
                            <el-tag v-if="item.row.status == 1" type="success">启用</el-tag>
                            <el-tag type="danger" v-else>禁用</el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template #default="item">
                        <div>
                            <el-button type="primary" circle :icon="Edit" @click="edit(item.row.uid)">
                            </el-button>
                            <el-button type="danger" circle :icon="Delete" @click="del(item.row.uid)">
                            </el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="prev, pager, next" :total="total" class="sumit" :page-size="size"
                @current-change="changePage">
            </el-pagination>
        </div>
    </div>
</template>

<script setup lang="ts">
import {
    Delete,
    Edit
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus'
import { defineEmits, computed } from "vue"
import { useStore } from "vuex"
import { userDel } from "../../request/api"
let store = useStore()
//去定义 子父组件通信方法 创建自定义事件名称
let emit = defineEmits(["edit"])
let edit = (uid: number) => {
    // 子传父
    emit("edit", uid)
}
// 点击删除
const del = (uid: number) => {
    ElMessageBox.confirm(
        '你确定要删除吗?',
        '提示',
        {
            distinguishCancelAndClose: true,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
        }
    )
        .then(() => {
            // 调用删除接口
            userDel({ uid }).then((res: any) => {
                if (res.data.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: res.data.msg
                    })
                    // 重新渲染列表
                    store.dispatch("user/userList_Action")
                    store.dispatch("user/userCount_Action")
                } else {
                    ElMessage({
                        type: 'error',
                        message: res.data.msg
                    })
                }
            })
        })
        .catch((action: Action) => {
            ElMessage({
                type: 'error',
                message: "取消删除"
            })
        })
}
// 获取管理员列表
const userList = computed(() =>
    store.getters["user/getUserList"]
)
store.dispatch("user/userList_Action")
// 获取总条目数
const total = computed(() =>
    store.getters["user/getTotal"]
)
store.dispatch("user/userCount_Action")
// 获取当页数量
const size = computed(() =>
    store.getters["user/getSize"]
)
// 点击切换页面
const changePage = (e: number) => {
    // 派遣行动
    store.dispatch("user/changePage_Action", e)
}
</script>

<style scoped>
.sumit {
    margin-top: 30px;
    position: absolute;
    right: 50px;
}
</style>
